package com.example.administrator.text;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import util.BitmapUtil;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.NetUtils;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.RoundImageView;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;

/**
 * 解除绑定
 */
public class RelieveBinDingActivity extends BaseActivity {
    private String mStringPone,mToken,mId,mBindingid;
    private String url;
    private UploadDialog mUploadDialog;

    @Bind(R.id.textView_relieve_name)   TextView  mTVName;              //Name
    @Bind(R.id.textView_relieve_phone)  TextView  mTVPhone;            //解除绑定的账号
    @Bind(R.id.imageView_relieve_head)  RoundImageView mImageViewHed;       //头像
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relieve_binding);
        ButterKnife.bind(this);

        mId = (String)SharedPreferencesUtils.getParam(this,HomeUtil.mUseridDB,"");
        mToken = (String)SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        initWeather();
        initImagerHead();

    }

    /**
     * 返回
     */
    @OnClick(R.id.image_relieve_return)
    void relieveReturn(){finish();}

    /**
     * 解除绑定
     */
    @OnClick(R.id.textView_relieve_return)
    void relieveBinDing(){
        mMap = new HashMap<>();
        mUploadDialog = new UploadDialog(this,"解绑中");
        mUploadDialog.show();
        mMap.put("bindingid",mBindingid);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngBinding_relieveBinding,stringJson,mToken,mId,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId,Url.mStirngBinding_relieveBinding,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = MyApplication.getApp().getFWQString();
        }
        getLogger().info("relieveBinDing()-解除绑定--->fwq--"+url+"stirngSign--"+stirngSign);
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    getLogger().info("relieveBinDing()-解除绑定--->onSuccess--->--String"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.getString("code");
                    LogUtil.eE("",response);
                    mUploadDialog.dismiss();
                    if (code.equals("1")) {
                        SharedPreferencesUtils.setParam(RelieveBinDingActivity.this, HomeUtil.mHSBinding, "no");
                        Toast.makeText(RelieveBinDingActivity.this, R.string.account_yes, Toast.LENGTH_SHORT).show();
                        sendBroadcast(new Intent(WeWiFiAccountActivity.mAction));          //发送一个广播
                        finish();

                    } else if (code.equals("0")) {
                        Toast.makeText(RelieveBinDingActivity.this, R.string.account_no, Toast.LENGTH_SHORT).show();

                    } else if (code.equals("-2")) {
                        CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(RelieveBinDingActivity.this);
                        crowdRetunDialog.show();
                    } else {
                        Toast.makeText(RelieveBinDingActivity.this, getString(R.string.FWQ), Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {e.printStackTrace();
                }
            }

            @Override public void onFailure(Throwable error) {
                mUploadDialog.dismiss();
                StringUtil.getStringUtilNew().getExceptionCode(RelieveBinDingActivity.this, error.toString());
            }
        });
    }

    /**
     * 如果刚注册绑定完就点击 week是为空  就从网络获取天气信息
     * 初始化天气
     */
    private void initWeather() {

        HomeUtil.getHemeUtilNew().getStatusBar(this,null);
        mStringPone = getIntent().getStringExtra("binDing");
        mBindingid = getIntent().getStringExtra("bindingid");
        mTVPhone.setText(mStringPone);
    }

    /**
     * 初始化头像和ViewPager
     */
    public void initImagerHead() {

        mMap = new HashMap<>();
        mMap.put("userid",mId);  mMap.put("token", mToken);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngMycenter_headImgUrl,stringJson,mToken,mId,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId,Url.mStirngMycenter_headImgUrl,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = MyApplication.getApp().getFWQString();
        }
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map, new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getString("code").equals("1")) {

                        String imageUri = jsonObject.getString("headurl");
                        mTVName.setText(jsonObject.getString("nickname"));
                        BitmapUtil.newBitmap().showBitmap(RelieveBinDingActivity.this,imageUri,mImageViewHed,R.mipmap.weaccount_head,null);
                    } else if (jsonObject.getString("code").equals("-1")) {
                        Toast.makeText(RelieveBinDingActivity.this, getString(R.string.FWQ), Toast.LENGTH_SHORT).show();
                    }else if (jsonObject.getString("code").equals("-2")){
                        CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(RelieveBinDingActivity.this);
                        crowdRetunDialog.show();
                    }

                } catch (Exception e) {e.printStackTrace();}
            }

            @Override public void onFailure(Throwable error) {
                Toast.makeText(RelieveBinDingActivity.this, getString(R.string.FWQ), Toast.LENGTH_SHORT).show();}});
    }

    @Override protected void onDestroy() {
        super.onDestroy();

        if(mMap !=null){mMap.clear();}

        if(mUploadDialog !=null){mUploadDialog.dismiss();}
    }
}
